package com.gyy.socket;


import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.*;

/**
 * TODO
 *
 * @author linfeng
 * @date 2021/4/13 17:50
 */
public class Service extends Thread {
    private Map<String, Socket> clientMap = new HashMap<>();

    public Service(){
    }

    public Service(Map<String,Socket> clientMap){
        this.clientMap=clientMap;
    }

    public void run() {
        while(true) {
            try {
                for(Map.Entry<String, Socket> entry : clientMap.entrySet()) {
                    //注册包 节点编号
                    String registrationPacket = entry.getKey();
                    Socket socket = entry.getValue();
                    assert socket != null;
                    InputStream is = socket.getInputStream();
                    OutputStream os = socket.getOutputStream();
                    if (is.available() != 0) {
                        byte[] bytes = ModBusUtils.readInputStream(is);
                        String str = ModBusUtils.bytes2HexString(bytes);
                        System.out.print("十六进制：" + str);
                        //截取对应的数据
                        List<Float> floatList = new ArrayList<>();
                        String[] arr = str.split(" ");
                        if (arr.length > 20) {
                            for (int i = 7; i < arr.length - 2; i += 4) {
                                String a = arr[i] + arr[i + 1] + arr[i + 2] + arr[i + 3];
                                Float f = Float.intBitsToFloat(Integer.valueOf(a, 16));
                                System.out.print("浮点数：" + f);
                                floatList.add(f);
                            }
                        }
                        System.out.println("节点编号：" + registrationPacket);
                        byte[] by = new byte[19];
                        by[0] = 55;
                        by[1] = 3;
                        by[2] = 16;
                        by[3] = 63;
                        by[4] = -128;
                        by[5] = 0;
                        by[6] = 0;
                        by[7] = 0;
                        by[8] = 0;
                        by[9] = 0;
                        by[10] = 0;
                        by[11] = 0;
                        by[12] = 0;
                        by[13] = 0;
                        by[14] = 0;
                        String hexStr = Integer.toHexString(Float.floatToIntBits(Float.parseFloat("2.0")));
                        byte[] b = ModBusUtils.hexStringToByteArray(hexStr);
                        by[15] = b[0];
                        by[16] = b[1];
                        by[17] = b[2];
                        by[18] = b[3];
                        //crc3校验
                        String crc = ModBusUtils.getCRC3(by);
                        byte[] crcByte = ModBusUtils.hexStringToByteArray(crc);
                        byte[] data = new byte[21];
                        data[19] = crcByte[0];
                        data[20] = crcByte[1];
                        for (int i = 0; i < by.length; i++) {
                            data[i] = by[i];
                        }
                        os.write(data);
                        System.out.println(registrationPacket + "：写入数据：" + Arrays.toString(data));
                    }
                    closeSocket(is,socket,os,entry.getKey());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void closeSocket(InputStream is,Socket socket,OutputStream os,String key) throws Exception {
        try {
            is.read();
        }catch (Exception e){
            e.printStackTrace();
            clientMap.remove(key);
            socket.close();
            is.close();
            os.close();
        }
    }
}


